package cn.cutie.clotcache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: plugins entrypoint
 * @Author: Cutie
 * @CreateDate: 2024/6/13 11:12
 * @Version: 0.0.1
 */
@Component
public class ClotApplicationListener implements ApplicationListener<ApplicationEvent> {
    @Autowired
    List<ClotPlugin> plugins;
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationReadyEvent are) {
            // 上下文准备好的事件
            // 之前：ApplicationRunner
            // 启动
            for (ClotPlugin plugin : plugins) {
                plugin.init();
                plugin.startup();
            }
        } else if (event instanceof ContextClosedEvent cce) {
            // 之前：@predestroy
            // 关闭
            for (ClotPlugin plugin : plugins) {
                plugin.shutdown();
            }
        }
    }
}
